package com.xiangmo.travel.Service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Dto.AttractionsDto;
import com.xiangmo.travel.Dto.PageInfoDto;
import com.xiangmo.travel.Dvo.AttractionsDvo;
import com.xiangmo.travel.Entity.Attractions;
import com.xiangmo.travel.Service.AttractionsCommentsService;
import com.xiangmo.travel.Service.AttractionsService;
import com.xiangmo.travel.Service.ScoreService;
import com.xiangmo.travel.mapper.AttractionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class AttractionsServiceImpl implements AttractionsService {
    @Autowired
    private AttractionsMapper attractionsMapper;
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private AttractionsCommentsService attractionsCommentsService;
    /**
     * 发布功能模块
     * @param attractionsDto 文章数据的封装
     * @return 执行结果
     */
    @Override
    public Long publishAttractions(AttractionsDto attractionsDto) {
        Attractions attractions = Attractions.builder()
                .userId(attractionsDto.getUserId())
                .description(attractionsDto.getDescription())
                .image(attractionsDto.getImage())
                .location(attractionsDto.getLocation())
                .title(attractionsDto.getTitle())
                .type(attractionsDto.getType().getPoints())
                .about(attractionsDto.getAbout())
                .trait(attractionsDto.getTrait())
                .fees(attractionsDto.getFees())
                .build();
        attractionsMapper.insert(attractions);
        return attractions.getId();
    }

    /**
     * 通过文章id查询该文章的详情信息
     * @param id
     * @return
     */
    @Override
    public Attractions getAttractionsById(Long id) {
        return attractionsMapper.selectById(id);
    }

    /**
     * 分页查询 通用
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfoDto<AttractionsDvo> attractionsPage(int pageNum, int pageSize) {
        // 构建分页对象
        Page<Attractions> page = new Page<>(pageNum, pageSize);
        // 查询数据并设置到分页对象中
        LambdaQueryWrapper<Attractions> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Attractions::getIsPublished, 1);
        attractionsMapper.selectPage(page, lambdaQueryWrapper);
        List<Attractions> list = page.getRecords();
        List<AttractionsDvo> attractionsDvoList = new ArrayList<>();
        //封装
        for(Attractions a : list){
            AttractionsDvo attractionsDvo = new AttractionsDvo();
            attractionsDvo.setId(a.getId());
            attractionsDvo.setTitle(a.getTitle());
            attractionsDvo.setDescription(a.getDescription());
            attractionsDvo.setUserId(a.getUserId());
            attractionsDvo.setType(a.getType());
            attractionsDvo.setImage(a.getImage());
            attractionsDvo.setLocation(a.getLocation());
            attractionsDvo.setCreatedAt(a.getCreatedAt());
            attractionsDvo.setUpdatedAt(a.getUpdatedAt());
            attractionsDvo.setIsPublished(a.getIsPublished());
            attractionsDvo.setAbout(a.getAbout());
            attractionsDvo.setTrait(a.getTrait());
            //分数
            attractionsDvo.setScore(scoreService.getScoreStar(a.getId()));
            attractionsDvo.setCommentAllNum(attractionsCommentsService.getAllCommentNum(a.getId()));
            attractionsDvoList.add(attractionsDvo);
        }
        PageInfoDto<AttractionsDvo> pageInfoDto = new PageInfoDto<>();
        pageInfoDto.setList(attractionsDvoList);
        pageInfoDto.setPages(page.getPages());
        pageInfoDto.setPageTotal(page.getTotal());
        pageInfoDto.setPageCurrent(page.getCurrent());
        pageInfoDto.setSize(page.getSize());
        return pageInfoDto;
    }

    /**
     * 分页查询 通过类型
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfoDto<AttractionsDvo> attractionsPageByType(int pageNum, int pageSize, String type) {
        // 构建分页对象
        Page<Attractions> page = new Page<>(pageNum, pageSize);
        // 查询数据并设置到分页对象中
        attractionsMapper.selectAllByType(page, type);
        List<Attractions> list = page.getRecords();
        List<AttractionsDvo> attractionsDvoList = new ArrayList<>();
        //封装
        for(Attractions a : list){
            AttractionsDvo attractionsDvo = new AttractionsDvo();
            attractionsDvo.setId(a.getId());
            attractionsDvo.setTitle(a.getTitle());
            attractionsDvo.setDescription(a.getDescription());
            attractionsDvo.setUserId(a.getUserId());
            attractionsDvo.setType(a.getType());
            attractionsDvo.setImage(a.getImage());
            attractionsDvo.setLocation(a.getLocation());
            attractionsDvo.setCreatedAt(a.getCreatedAt());
            attractionsDvo.setUpdatedAt(a.getUpdatedAt());
            attractionsDvo.setIsPublished(a.getIsPublished());
            attractionsDvo.setAbout(a.getAbout());
            attractionsDvo.setTrait(a.getTrait());
            //分数
            attractionsDvo.setScore(scoreService.getScoreStar(a.getId()));
            attractionsDvo.setCommentAllNum(attractionsCommentsService.getAllCommentNum(a.getId()));
            attractionsDvoList.add(attractionsDvo);
        }
        PageInfoDto<AttractionsDvo> pageInfoDto = new PageInfoDto<>();
        pageInfoDto.setList(attractionsDvoList);
        pageInfoDto.setPages(page.getPages());
        pageInfoDto.setPageTotal(page.getTotal());
        pageInfoDto.setPageCurrent(page.getCurrent());
        pageInfoDto.setSize(page.getSize());
        return pageInfoDto;
    }

    //搜索
    @Override
    public PageInfoDto<AttractionsDvo> searchAttractions(int pageNum, int pageSize, String content) {
        Page<Attractions> page = new Page<>(pageNum, pageSize);
        attractionsMapper.searchAttractions(page, content);
        List<Attractions> list = page.getRecords();
        List<AttractionsDvo> attractionsDvoList = new ArrayList<>();
        //封装
        for(Attractions a : list){
            AttractionsDvo attractionsDvo = new AttractionsDvo();
            attractionsDvo.setId(a.getId());
            attractionsDvo.setTitle(a.getTitle());
            attractionsDvo.setDescription(a.getDescription());
            attractionsDvo.setUserId(a.getUserId());
            attractionsDvo.setType(a.getType());
            attractionsDvo.setImage(a.getImage());
            attractionsDvo.setLocation(a.getLocation());
            attractionsDvo.setCreatedAt(a.getCreatedAt());
            attractionsDvo.setUpdatedAt(a.getUpdatedAt());
            attractionsDvo.setIsPublished(a.getIsPublished());
            attractionsDvo.setAbout(a.getAbout());
            attractionsDvo.setTrait(a.getTrait());
            //分数
            attractionsDvo.setScore(scoreService.getScoreStar(a.getId()));
            attractionsDvo.setCommentAllNum(attractionsCommentsService.getAllCommentNum(a.getId()));
            attractionsDvoList.add(attractionsDvo);
        }
        PageInfoDto<AttractionsDvo> pageInfoDto = new PageInfoDto<>();
        pageInfoDto.setList(attractionsDvoList);
        pageInfoDto.setPages(page.getPages());
        pageInfoDto.setPageTotal(page.getTotal());
        pageInfoDto.setPageCurrent(page.getCurrent());
        pageInfoDto.setSize(page.getSize());
        return pageInfoDto;
    }

    @Override
    public PageInfoDto<AttractionsDvo> searchAttractionsByType(int pageNum, int pageSize, String content, String type) {
        Page<Attractions> page = new Page<>(pageNum, pageSize);
        attractionsMapper.searchAttractionsByType(page, content, type);
        List<Attractions> list = page.getRecords();
        List<AttractionsDvo> attractionsDvoList = new ArrayList<>();
        //封装
        for(Attractions a : list){
            AttractionsDvo attractionsDvo = new AttractionsDvo();
            attractionsDvo.setId(a.getId());
            attractionsDvo.setTitle(a.getTitle());
            attractionsDvo.setDescription(a.getDescription());
            attractionsDvo.setUserId(a.getUserId());
            attractionsDvo.setType(a.getType());
            attractionsDvo.setImage(a.getImage());
            attractionsDvo.setLocation(a.getLocation());
            attractionsDvo.setCreatedAt(a.getCreatedAt());
            attractionsDvo.setUpdatedAt(a.getUpdatedAt());
            attractionsDvo.setIsPublished(a.getIsPublished());
            attractionsDvo.setAbout(a.getAbout());
            attractionsDvo.setTrait(a.getTrait());
            //分数
            attractionsDvo.setScore(scoreService.getScoreStar(a.getId()));
            attractionsDvo.setCommentAllNum(attractionsCommentsService.getAllCommentNum(a.getId()));
            attractionsDvoList.add(attractionsDvo);
        }
        PageInfoDto<AttractionsDvo> pageInfoDto = new PageInfoDto<>();
        pageInfoDto.setList(attractionsDvoList);
        pageInfoDto.setPages(page.getPages());
        pageInfoDto.setPageTotal(page.getTotal());
        pageInfoDto.setPageCurrent(page.getCurrent());
        pageInfoDto.setSize(page.getSize());
        return pageInfoDto;
    }

    @Override
    public Page<Attractions> getAttractionsByUserId(Long userId, int pageNum, int pageSize) {
        LambdaQueryWrapper<Attractions> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Attractions::getUserId, userId);
        Page<Attractions> page = new Page<>(pageNum, pageSize);
        attractionsMapper.selectPage(page, lambdaQueryWrapper);
        return page;
    }


}
